define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Mypoint, $window, $timeout, $ionicScrollDelegate, GoPage, GoDialog,$ionicModal) {
		
		$scope.type = "";//默认加载全部
		$scope.isLoad = true; //不可以加载
		
		//画面加载
		$scope.$on('$ionicView.afterEnter', function(scope, status) {
			var all = $window.innerHeight;
			$scope.myObj = {
				"height": all - 44 - 20 - 68 - 42 + "px"
			};
			$scope.init_data();
		}, false);
		
		//检索商品
		$scope.searchGoods = function(type){
			$scope.firstHide = false;
			$ionicScrollDelegate.$getByHandle('goodsScroll').scrollTop();//回到顶部
			$scope.type = type;//控制样式
			Mypoint.getGoodsList(type,1,function(data){
				$scope.goodsList = data.goods;
				$scope.page =  data.page;
				$scope.nPage = 1;
				$scope.firstHide = true;
				$scope.isLoad = false; //可以加载，防止没有数据时请求上拉事件
//				console.log(data)
			})
		}
		
		//初始化
		$scope.init_data = function(){
			//总积分
			Mypoint.getPointList(function(data){
				$scope.point = data;
			})
			//商品类型
			Mypoint.getGoodsType(function(data){
				if(!data) return;
				$scope.goodsTypes = data.type;
//				$scope.goodsTypes = [{name:'电脑'},{name:'笔记本电脑笔记本电脑'},{name:'电脑A'},{name:'笔记本电脑A'},{name:'电脑B'},{name:'笔记本电脑B'}];
				$scope.widthLength = ($scope.goodsTypes.length+1)*100;//ion-scroll的宽度
				var allWidth = $window.innerWidth;
				if($scope.widthLength<allWidth){//如果个数较少，则平分屏幕
					$scope.everyLength = allWidth/($scope.goodsTypes.length+1);
				}else{
					$scope.everyLength = 100;
				}
			})
			//商品列表
			$scope.searchGoods($scope.type);
		}
		
		/***********显示《积分规则》**********/
		$ionicModal.fromTemplateUrl("templates/modal/point_rule.html", {
			scope: $scope,
			animation: "slide-in-up"
		}).then(function(modal) {
			$scope.modal_rule = modal;
		});
		//打开
		$scope.openModal_rule = function() {
			$scope.modal_rule.show();
		}
		//隐藏
		$scope.closeModal_rule = function() {
			$scope.modal_rule.hide();
		}
		/***********显示《积分规则》**********/
		
		// 积分明细画面跳转
		$scope.show_detail = function() {
			GoPage.go_states("mypointdetail");
		}
		
		// 兑换记录画面跳转
		$scope.show_record = function() {
			GoPage.go_states("exchangerecord");
		}
		
		// 商品详情画面跳转
		$scope.gotoGoods = function(goods_id) {
			GoPage.go_states("pointgoods", {
				id: goods_id
			});
		}
		
		/****************上下拉刷新****************/
		$scope.doRefresh = function() {
			//防止重复加载
			$scope.isLoad = true;
			Mypoint.getGoodsList($scope.type, 1, function(data) {
				$scope.goodsList = data.goods;
				$scope.nPage = 1;
			},function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		};
		//加载更多
		$scope.doRefreshMore = function() {
			//防止重复加载
			$scope.isLoad = true;
			Mypoint.getGoodsList($scope.type, $scope.nPage + 1, function(data) {
				if(data != null&&data.goods) {
					if($scope.goodsList != undefined) {
						$scope.goodsList = $scope.goodsList.concat(data.goods);
						$scope.nPage += 1;
					} else {
						$scope.goodsList = data.goods;
					}
				}
			},function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		};
		/****************上下拉刷新****************/
		
		
		
		
	}

	controller.$inject = ['$scope', 'Mypoint', '$window', '$timeout', '$ionicScrollDelegate', 'GoPage', 'GoDialog','$ionicModal'];

	return app.registerController("mypointctrl",controller);
});